from get_basic_info_class import *
from my_json import *
from spider_url import id_json_url, school_json_url, province_url

basic_path = str(os.path.dirname(__file__)) + "/"
download_path = bulid_download_path(basic_path)


def init_id():
    j = download_json(id_json_url, download_path + "id.json")
    special_dict = {}
    for i in j["data"]["special"]:
        special_dict[i["id"]] = i["name"]
    save_json(basic_path + "special_id.json", special_dict)


def init_province():
    url = province_url()
    j = url.province
    province_dict = {}
    for k, v in j.items():
        p = province(v, k)
        h = {}
        try:
            high_school = download_json(url.high_school_to_string(k), download_path + "province" + str(k) + ".json")
            for kh, vh in high_school["data"].items():
                h[kh] = []
                for i in vh:
                    h[kh].append(i["name"])
            p.high_school = h
        except:
            pass
        province_dict[k] = p.to_dict()
    save_json(basic_path + "province_id.json", province_dict)



def init_school():
    j = download_json(id_json_url, download_path + "id.json")
    school_dict = {}
    for i in j["data"]["school"]:
        school_dict[i["school_id"]] = i["name"]
    j = download_json(school_json_url, download_path + "school.json")
    for i in j["data"]:
        if school_dict[i["school_id"]] != i["name"]:
            raise ValueError("school.py name not match")
        s = school(i["name"], i['school_id'], i["type"], i["proid"])
        school_dict[i['school_id']] = s.to_dict()
    save_json(basic_path + "school_id.json", school_dict)


if __name__ == "__main__":
    # download()
    init_id()
    init_province()
    init_school()
